{% extends "base.html" %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>运输管理</h2>
        <a href="{{ url_for('storage.transport_add') }}" class="btn btn-primary">
            <i class="fas fa-plus"></i> 创建运输任务
        </a>
    </div>

    <!-- 搜索表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label">车牌号</label>
                    <input type="text" class="form-control" name="vehicle_number" value="{{ vehicle_number }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">司机姓名</label>
                    <input type="text" class="form-control" name="driver_name" value="{{ driver_name }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">状态</label>
                    <select class="form-select" name="status">
                        <option value="">所有状态</option>
                        <option value="preparing" {% if status == 'preparing' %}selected{% endif %}>准备中</option>
                        <option value="in_transit" {% if status == 'in_transit' %}selected{% endif %}>运输中</option>
                        <option value="completed" {% if status == 'completed' %}selected{% endif %}>已完成</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">开始日期</label>
                    <input type="date" class="form-control" name="start_date" value="{{ start_date }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">&nbsp;</label>
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>车牌号</th>
                            <th>司机</th>
                            <th>联系电话</th>
                            <th>起点</th>
                            <th>终点</th>
                            <th>开始时间</th>
                            <th>预计到达</th>
                            <th>当前温度</th>
                            <th>当前湿度</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for transport in transports %}
                        <tr>
                            <td>{{ transport.vehicle_number }}</td>
                            <td>{{ transport.driver_name }}</td>
                            <td>{{ transport.driver_phone }}</td>
                            <td>{{ transport.start_location }}</td>
                            <td>{{ transport.end_location }}</td>
                            <td>{{ transport.start_time.strftime('%Y-%m-%d %H:%M') }}</td>
                            <td>{{ transport.estimated_arrival_time.strftime('%Y-%m-%d %H:%M') }}</td>
                            <td>{{ transport.current_temperature }}°C</td>
                            <td>{{ transport.current_humidity }}%</td>
                            <td>
                                {% if transport.status == 'preparing' %}
                                    <span class="badge bg-warning">准备中</span>
                                {% elif transport.status == 'in_transit' %}
                                    <span class="badge bg-info">运输中</span>
                                {% elif transport.status == 'completed' %}
                                    <span class="badge bg-success">已完成</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{{ url_for('storage.transport_edit', id=transport.id) }}" class="btn btn-sm btn-info">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                <a href="{{ url_for('storage.transport_delete', id=transport.id) }}" 
                                   class="btn btn-sm btn-danger"
                                   onclick="return confirm('确定要删除该运输任务吗？')">
                                    <i class="fas fa-trash"></i> 删除
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if pagination and pagination.pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('storage.transport_list', page=pagination.prev_num, vehicle_number=vehicle_number, driver_name=driver_name, status=status, start_date=start_date) if pagination.has_prev else '#' }}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                    {% for page in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                        {% if page %}
                            <li class="page-item {% if page == pagination.page %}active{% endif %}">
                                <a class="page-link" href="{{ url_for('storage.transport_list', page=page, vehicle_number=vehicle_number, driver_name=driver_name, status=status, start_date=start_date) }}">
                                    {{ page }}
                                </a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                        {% endif %}
                    {% endfor %}
                    <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('storage.transport_list', page=pagination.next_num, vehicle_number=vehicle_number, driver_name=driver_name, status=status, start_date=start_date) if pagination.has_next else '#' }}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                </ul>
            </nav>
            <div class="text-center text-muted">
                共 {{ pagination.total }} 条记录，当前第 {{ pagination.page }}/{{ pagination.pages }} 页
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %} 